package cn.yl.caner.web;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import cn.yl.caner.domain.ChangeData;
import cn.yl.caner.service.ChangeService;


public class ChangeServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

		//改变帐号资料
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//web层，servlet层收集动态信息
				request.setCharacterEncoding("UTF-8");
				Map<String, String[]> changedata = request.getParameterMap();
				ChangeData change = new ChangeData();
				try {
					BeanUtils.populate(change, changedata);
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					System.out.println("获取数据错误1");
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					System.out.println("获取数据错误2");
				}
				ChangeService changeservice = new ChangeService();
				int i = changeservice.change(change);
				//对从Dao层返回的信息进行判断并重定向
				if(i == 0) {
					System.out.println("修改失败");
//					response.sendRedirect(request.getContextPath()+"/fail.html");
				}else {
					System.out.println("修改成功");
					response.sendRedirect(request.getContextPath()+"/account.jsp");
				}
			}


	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}